package c.c.b.a.b.a.h;

import c.c.b.a.a.e;
import c.c.b.a.a.v;
import c.c.b.a.a.w;
import c.c.b.a.a.x;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Objects;

/* compiled from: Http2Stream */
public final class q {
    public long a = 0;
    public long b;
    public final int c;
    public final g d;
    public final List<c> e;
    public List<c> f;
    public boolean g;
    public final b h;
    public final a i;
    public final c j = new c();
    public final c k = new c();
    public b l = null;

    /* compiled from: Http2Stream */
    public final class a implements v {
        public final e a = new e();
        public boolean b;
        public boolean c;

        public x a() {
            return q.this.k;
        }

        /* JADX WARNING: Missing block: B:8:0x000a, code skipped:
            r0 = r13.d;
     */
        /* JADX WARNING: Missing block: B:9:0x0011, code skipped:
            if (r0.i.c != false) goto L_0x0034;
     */
        /* JADX WARNING: Missing block: B:11:0x001b, code skipped:
            if (r13.a.b <= 0) goto L_0x0029;
     */
        /* JADX WARNING: Missing block: B:13:0x0023, code skipped:
            if (r13.a.b <= 0) goto L_0x0034;
     */
        /* JADX WARNING: Missing block: B:14:0x0025, code skipped:
            k(true);
     */
        /* JADX WARNING: Missing block: B:15:0x0029, code skipped:
            r0.d.s(r0.c, true, null, 0);
     */
        /* JADX WARNING: Missing block: B:16:0x0034, code skipped:
            r1 = r13.d;
     */
        /* JADX WARNING: Missing block: B:17:0x0036, code skipped:
            monitor-enter(r1);
     */
        /* JADX WARNING: Missing block: B:19:?, code skipped:
            r13.b = true;
     */
        /* JADX WARNING: Missing block: B:20:0x0039, code skipped:
            monitor-exit(r1);
     */
        /* JADX WARNING: Missing block: B:21:0x003a, code skipped:
            r13.d.d.p.v();
            r13.d.g();
     */
        /* JADX WARNING: Missing block: B:22:0x0048, code skipped:
            return;
     */
        public void close() throws java.io.IOException {
            /*
            r13 = this;
            r0 = c.c.b.a.b.a.h.q.this;
            monitor-enter(r0);
            r1 = r13.b;	 Catch:{ all -> 0x004c }
            if (r1 == 0) goto L_0x0009;
        L_0x0007:
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            return;
        L_0x0009:
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            r0 = c.c.b.a.b.a.h.q.this;
            r1 = r0.i;
            r1 = r1.c;
            r2 = 1;
            if (r1 != 0) goto L_0x0034;
        L_0x0013:
            r1 = r13.a;
            r3 = r1.b;
            r5 = 0;
            r1 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1));
            if (r1 <= 0) goto L_0x0029;
        L_0x001d:
            r0 = r13.a;
            r0 = r0.b;
            r3 = (r0 > r5 ? 1 : (r0 == r5 ? 0 : -1));
            if (r3 <= 0) goto L_0x0034;
        L_0x0025:
            r13.k(r2);
            goto L_0x001d;
        L_0x0029:
            r7 = r0.d;
            r8 = r0.c;
            r9 = 1;
            r10 = 0;
            r11 = 0;
            r7.s(r8, r9, r10, r11);
        L_0x0034:
            r1 = c.c.b.a.b.a.h.q.this;
            monitor-enter(r1);
            r13.b = r2;	 Catch:{ all -> 0x0049 }
            monitor-exit(r1);	 Catch:{ all -> 0x0049 }
            r0 = c.c.b.a.b.a.h.q.this;
            r0 = r0.d;
            r0 = r0.p;
            r0.v();
            r0 = c.c.b.a.b.a.h.q.this;
            r0.g();
            return;
        L_0x0049:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x0049 }
            throw r0;
        L_0x004c:
            r1 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            throw r1;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.b.a.h.q$a.close():void");
        }

        public void flush() throws IOException {
            synchronized (q.this) {
                q.this.h();
            }
            while (this.a.b > 0) {
                k(false);
                q.this.d.w();
            }
        }

        public void g0(e eVar, long j) throws IOException {
            this.a.g0(eVar, j);
            while (this.a.b >= 16384) {
                k(false);
            }
        }

        /* JADX WARNING: Missing block: B:16:?, code skipped:
            r1.k.n();
            r11.d.h();
            r9 = java.lang.Math.min(r11.d.b, r11.a.b);
            r1 = r11.d;
            r1.b -= r9;
     */
        public final void k(boolean r12) throws java.io.IOException {
            /*
            r11 = this;
            r0 = c.c.b.a.b.a.h.q.this;
            monitor-enter(r0);
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007b }
            r1 = r1.k;	 Catch:{ all -> 0x007b }
            r1.h();	 Catch:{ all -> 0x007b }
        L_0x000a:
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0072 }
            r2 = r1.b;	 Catch:{ all -> 0x0072 }
            r4 = 0;
            r6 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1));
            if (r6 > 0) goto L_0x0024;
        L_0x0014:
            r2 = r11.c;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x0018:
            r2 = r11.b;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x001c:
            r2 = r1.l;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x0020:
            r1.i();	 Catch:{ all -> 0x0072 }
            goto L_0x000a;
        L_0x0024:
            r1 = r1.k;	 Catch:{ all -> 0x007b }
            r1.n();	 Catch:{ all -> 0x007b }
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007b }
            r1.h();	 Catch:{ all -> 0x007b }
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007b }
            r1 = r1.b;	 Catch:{ all -> 0x007b }
            r3 = r11.a;	 Catch:{ all -> 0x007b }
            r3 = r3.b;	 Catch:{ all -> 0x007b }
            r9 = java.lang.Math.min(r1, r3);	 Catch:{ all -> 0x007b }
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007b }
            r2 = r1.b;	 Catch:{ all -> 0x007b }
            r2 = r2 - r9;
            r1.b = r2;	 Catch:{ all -> 0x007b }
            monitor-exit(r0);	 Catch:{ all -> 0x007b }
            r0 = r1.k;
            r0.h();
            r0 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0069 }
            r5 = r0.d;	 Catch:{ all -> 0x0069 }
            r6 = r0.c;	 Catch:{ all -> 0x0069 }
            if (r12 == 0) goto L_0x005a;
        L_0x004f:
            r12 = r11.a;	 Catch:{ all -> 0x0069 }
            r0 = r12.b;	 Catch:{ all -> 0x0069 }
            r12 = (r9 > r0 ? 1 : (r9 == r0 ? 0 : -1));
            if (r12 != 0) goto L_0x005a;
        L_0x0057:
            r12 = 1;
            r7 = 1;
            goto L_0x005c;
        L_0x005a:
            r12 = 0;
            r7 = 0;
        L_0x005c:
            r8 = r11.a;	 Catch:{ all -> 0x0069 }
            r5.s(r6, r7, r8, r9);	 Catch:{ all -> 0x0069 }
            r12 = c.c.b.a.b.a.h.q.this;
            r12 = r12.k;
            r12.n();
            return;
        L_0x0069:
            r12 = move-exception;
            r0 = c.c.b.a.b.a.h.q.this;
            r0 = r0.k;
            r0.n();
            throw r12;
        L_0x0072:
            r12 = move-exception;
            r1 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007b }
            r1 = r1.k;	 Catch:{ all -> 0x007b }
            r1.n();	 Catch:{ all -> 0x007b }
            throw r12;	 Catch:{ all -> 0x007b }
        L_0x007b:
            r12 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x007b }
            throw r12;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.b.a.h.q$a.k(boolean):void");
        }
    }

    /* compiled from: Http2Stream */
    public final class b implements w {
        public final e a = new e();
        public final e b = new e();
        public final long c;
        public boolean d;
        public boolean e;

        public b(long j) {
            this.c = j;
        }

        public x a() {
            return q.this.j;
        }

        /* JADX WARNING: Missing block: B:18:0x0050, code skipped:
            r10 = r7.f.d;
     */
        /* JADX WARNING: Missing block: B:19:0x0054, code skipped:
            monitor-enter(r10);
     */
        /* JADX WARNING: Missing block: B:21:?, code skipped:
            r2 = r7.f.d;
            r3 = r2.j + r8;
            r2.j = r3;
     */
        /* JADX WARNING: Missing block: B:22:0x0069, code skipped:
            if (r3 < ((long) (r2.l.b() / 2))) goto L_0x007b;
     */
        /* JADX WARNING: Missing block: B:23:0x006b, code skipped:
            r2 = r7.f.d;
            r2.q(0, r2.j);
            r7.f.d.j = 0;
     */
        /* JADX WARNING: Missing block: B:24:0x007b, code skipped:
            monitor-exit(r10);
     */
        /* JADX WARNING: Missing block: B:25:0x007c, code skipped:
            return r8;
     */
        public long b(c.c.b.a.a.e r8, long r9) throws java.io.IOException {
            /*
            r7 = this;
            r0 = 0;
            r2 = (r9 > r0 ? 1 : (r9 == r0 ? 0 : -1));
            if (r2 < 0) goto L_0x0095;
        L_0x0006:
            r2 = c.c.b.a.b.a.h.q.this;
            monitor-enter(r2);
            r7.n();	 Catch:{ all -> 0x0092 }
            r3 = r7.d;	 Catch:{ all -> 0x0092 }
            if (r3 != 0) goto L_0x008a;
        L_0x0010:
            r3 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0092 }
            r3 = r3.l;	 Catch:{ all -> 0x0092 }
            if (r3 != 0) goto L_0x0080;
        L_0x0016:
            r3 = r7.b;	 Catch:{ all -> 0x0092 }
            r4 = r3.b;	 Catch:{ all -> 0x0092 }
            r6 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1));
            if (r6 != 0) goto L_0x0022;
        L_0x001e:
            r8 = -1;
            monitor-exit(r2);	 Catch:{ all -> 0x0092 }
            return r8;
        L_0x0022:
            r9 = java.lang.Math.min(r9, r4);	 Catch:{ all -> 0x0092 }
            r8 = r3.b(r8, r9);	 Catch:{ all -> 0x0092 }
            r10 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0092 }
            r3 = r10.a;	 Catch:{ all -> 0x0092 }
            r3 = r3 + r8;
            r10.a = r3;	 Catch:{ all -> 0x0092 }
            r10 = r10.d;	 Catch:{ all -> 0x0092 }
            r10 = r10.l;	 Catch:{ all -> 0x0092 }
            r10 = r10.b();	 Catch:{ all -> 0x0092 }
            r10 = r10 / 2;
            r5 = (long) r10;	 Catch:{ all -> 0x0092 }
            r10 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1));
            if (r10 < 0) goto L_0x004f;
        L_0x0040:
            r10 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0092 }
            r3 = r10.d;	 Catch:{ all -> 0x0092 }
            r4 = r10.c;	 Catch:{ all -> 0x0092 }
            r5 = r10.a;	 Catch:{ all -> 0x0092 }
            r3.q(r4, r5);	 Catch:{ all -> 0x0092 }
            r10 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0092 }
            r10.a = r0;	 Catch:{ all -> 0x0092 }
        L_0x004f:
            monitor-exit(r2);	 Catch:{ all -> 0x0092 }
            r10 = c.c.b.a.b.a.h.q.this;
            r10 = r10.d;
            monitor-enter(r10);
            r2 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007d }
            r2 = r2.d;	 Catch:{ all -> 0x007d }
            r3 = r2.j;	 Catch:{ all -> 0x007d }
            r3 = r3 + r8;
            r2.j = r3;	 Catch:{ all -> 0x007d }
            r2 = r2.l;	 Catch:{ all -> 0x007d }
            r2 = r2.b();	 Catch:{ all -> 0x007d }
            r2 = r2 / 2;
            r5 = (long) r2;	 Catch:{ all -> 0x007d }
            r2 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1));
            if (r2 < 0) goto L_0x007b;
        L_0x006b:
            r2 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007d }
            r2 = r2.d;	 Catch:{ all -> 0x007d }
            r3 = 0;
            r4 = r2.j;	 Catch:{ all -> 0x007d }
            r2.q(r3, r4);	 Catch:{ all -> 0x007d }
            r2 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x007d }
            r2 = r2.d;	 Catch:{ all -> 0x007d }
            r2.j = r0;	 Catch:{ all -> 0x007d }
        L_0x007b:
            monitor-exit(r10);	 Catch:{ all -> 0x007d }
            return r8;
        L_0x007d:
            r8 = move-exception;
            monitor-exit(r10);	 Catch:{ all -> 0x007d }
            throw r8;
        L_0x0080:
            r8 = new c.c.b.a.b.a.h.w;	 Catch:{ all -> 0x0092 }
            r9 = c.c.b.a.b.a.h.q.this;	 Catch:{ all -> 0x0092 }
            r9 = r9.l;	 Catch:{ all -> 0x0092 }
            r8.m917init(r9);	 Catch:{ all -> 0x0092 }
            throw r8;	 Catch:{ all -> 0x0092 }
        L_0x008a:
            r8 = new java.io.IOException;	 Catch:{ all -> 0x0092 }
            r9 = "stream closed";
            r8.<init>(r9);	 Catch:{ all -> 0x0092 }
            throw r8;	 Catch:{ all -> 0x0092 }
        L_0x0092:
            r8 = move-exception;
            monitor-exit(r2);	 Catch:{ all -> 0x0092 }
            throw r8;
        L_0x0095:
            r8 = new java.lang.IllegalArgumentException;
            r0 = "byteCount < 0: ";
            r9 = c.b.a.a.a.B(r0, r9);
            r8.<init>(r9);
            throw r8;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.b.a.h.q$b.b(c.c.b.a.a.e, long):long");
        }

        public void close() throws IOException {
            synchronized (q.this) {
                this.d = true;
                this.b.B0();
                q.this.notifyAll();
            }
            q.this.g();
        }

        public final void n() throws IOException {
            q.this.j.h();
            while (this.b.b == 0 && !this.e && !this.d) {
                try {
                    q qVar = q.this;
                    if (qVar.l != null) {
                        break;
                    }
                    qVar.i();
                } catch (Throwable th) {
                    q.this.j.n();
                }
            }
            q.this.j.n();
        }
    }

    /* compiled from: Http2Stream */
    public class c extends c.c.b.a.a.c {
        public void j() {
            q qVar = q.this;
            b bVar = b.CANCEL;
            if (qVar.d(bVar)) {
                qVar.d.r(qVar.c, bVar);
            }
        }

        public IOException k(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        public void n() throws IOException {
            if (l()) {
                throw k(null);
            }
        }
    }

    public q(int i, g gVar, boolean z, boolean z2, List<c> list) {
        Objects.requireNonNull(gVar, "connection == null");
        Objects.requireNonNull(list, "requestHeaders == null");
        this.c = i;
        this.d = gVar;
        this.b = (long) gVar.m.b();
        b bVar = new b((long) gVar.l.b());
        this.h = bVar;
        a aVar = new a();
        this.i = aVar;
        bVar.e = z2;
        aVar.c = z;
        this.e = list;
    }

    public void a(b bVar) throws IOException {
        if (d(bVar)) {
            g gVar = this.d;
            gVar.p.n(this.c, bVar);
        }
    }

    public synchronized boolean b() {
        if (this.l != null) {
            return false;
        }
        b bVar = this.h;
        if (bVar.e || bVar.d) {
            a aVar = this.i;
            if ((aVar.c || aVar.b) && this.g) {
                return false;
            }
        }
        return true;
    }

    public boolean c() {
        if (this.d.a == ((this.c & 1) == 1)) {
            return true;
        }
        return false;
    }

    public final boolean d(b bVar) {
        synchronized (this) {
            if (this.l != null) {
                return false;
            } else if (this.h.e && this.i.c) {
                return false;
            } else {
                this.l = bVar;
                notifyAll();
                this.d.v(this.c);
                return true;
            }
        }
    }

    public v e() {
        synchronized (this) {
            if (!this.g) {
                if (!c()) {
                    throw new IllegalStateException("reply before requesting the sink");
                }
            }
        }
        return this.i;
    }

    public void f() {
        boolean b;
        synchronized (this) {
            this.h.e = true;
            b = b();
            notifyAll();
        }
        if (!b) {
            this.d.v(this.c);
        }
    }

    public void g() throws IOException {
        Object obj;
        boolean b;
        synchronized (this) {
            b bVar = this.h;
            if (!bVar.e && bVar.d) {
                a aVar = this.i;
                if (aVar.c || aVar.b) {
                    obj = 1;
                    b = b();
                }
            }
            obj = null;
            b = b();
        }
        if (obj != null) {
            a(b.CANCEL);
        } else if (!b) {
            this.d.v(this.c);
        }
    }

    public void h() throws IOException {
        a aVar = this.i;
        if (aVar.b) {
            throw new IOException("stream closed");
        } else if (aVar.c) {
            throw new IOException("stream finished");
        } else if (this.l != null) {
            throw new w(this.l);
        }
    }

    public void i() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            throw new InterruptedIOException();
        }
    }
}
